package org.github.manx98.jlibhac.fssystem;

import lombok.Data;
import org.github.manx98.jlibhac.common.reflect.StructureMap;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/6 14:38
 */
@Data
public class NodeHeader {
    @StructureMap(offset = 0)
    public int Index;
    @StructureMap(offset = 4)
    public int EntryCount;
    @StructureMap(offset = 8)
    public long OffsetEnd;

    public void verify(int nodeIndex, long nodeSize, long entrySize) {
        if (Index != nodeIndex)
            throw new IllegalArgumentException("InvalidBucketTreeNodeIndex, excepted " + nodeIndex + ", but got " + Index);

        if (entrySize == 0 || nodeSize < entrySize + BucketTree.headerSize)
            throw new IllegalArgumentException("InvalidSize:  " + entrySize);

        long maxEntryCount = (nodeSize - BucketTree.headerSize) / entrySize;

        if (EntryCount <= 0 || maxEntryCount < EntryCount)
            throw new IllegalArgumentException("InvalidBucketTreeNodeEntryCount: " + EntryCount);

        if (OffsetEnd < 0)
            throw new IllegalArgumentException("InvalidBucketTreeNodeOffset: " + OffsetEnd);

    }
}
